#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: zhengpingping

import os
import re
import sys
import shutil

reload(sys)
sys.setdefaultencoding('utf-8')
lua_path = '../../../../xLua_master/Assets/Lua/NetWork/PBLua/'

#查找的文件后缀
search_text = [
	'.lua'
]

#内部使用
search_docs = []

#创建目录
def create_dir(folder):
	if not os.path.isdir(folder):
		os.makedirs(folder)

#删除目录
def delete_dir(folder):
	if os.path.isdir(folder):
		shutil.rmtree(folder)

# 初始化方法
def init():
	delete_dir(lua_path);
	create_dir(lua_path);

	print('Start Create lua File');
	for dirpath, dirnames, filenames in os.walk('./'):
		for filename in filenames:
			name, text = os.path.splitext(filename)
			if len(search_text):
				if text in search_text:
					temp =  {}
					temp['name'] = name
					temp['filename'] = filename
					search_docs.append(temp)
			else:
				print(filename + 'not lua file please check it')
				os.system('pause')

	foo = open(lua_path + "/RequireAllPB" + ".lua", "w+")
	foo.write("""-- Generated Tool Do not Edit""")
	foo.write(unicode('\r'))
	for doc in search_docs:
		foo.write(unicode('\n'))
		foo.write("require 'NetWork/PBLua/" + doc['name'] + "'")
		shutil.copy(doc['filename'], lua_path); 
		print(doc['name'] + ' translate finished')
		
	foo.close()
	print('RequireAllPB translate finished')

init()
print("Create lua Succeed!!!");